package com.dunqi.gpm.shunqing.my.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dunqi.gpm.shunqing.KongApp;
import com.dunqi.gpm.shunqing.MainActivity;
import com.dunqi.gpm.shunqing.R;
import com.dunqi.gpm.shunqing.common.base.BaseFragment;
import com.dunqi.gpm.shunqing.my.LoginActivity;
import com.dunqi.gpm.shunqing.my.activity.NotifyPasswordActivity;
import com.dunqi.gpm.shunqing.my.bean.User;
import com.dunqi.gpm.shunqing.my.util.UserUtil;
import com.pgyersdk.javabean.AppBean;
import com.pgyersdk.update.PgyUpdateManager;
import com.pgyersdk.update.UpdateManagerListener;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/11/27.
 */

public class MyFragment extends BaseFragment {

    @BindView(R.id.state_view)
    FrameLayout stateView;
    @BindView(R.id.head_iv)
    ImageView headIv;
    @BindView(R.id.nick_name_tv)
    TextView nick_name_tv;
    @BindView(R.id.office_tv)
    TextView office_tv;
    @BindView(R.id.message_ll)
    LinearLayout messageLl;
    @BindView(R.id.check_password_ll)
    LinearLayout checkPasswordLl;
    @BindView(R.id.check_out_user_ll)
    LinearLayout checkOutUserLl;

    @Override
    protected void refreshData(Bundle savedInstanceState) {

    }

    @Override
    protected void initUiAndListener() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            stateView.setVisibility(View.GONE);
        }

        User user = UserUtil.getUser();
        nick_name_tv.setText(user.getUserName());
        String orgname = user.getOrgName();
        if (TextUtils.isEmpty(orgname)) {
            office_tv.setVisibility(View.GONE);
        } else {
            office_tv.setVisibility(View.VISIBLE);
            office_tv.setText(orgname);
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_my;
    }

    @OnClick({R.id.message_ll, R.id.check_password_ll, R.id.check_out_user_ll, R.id.update_ll})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.update_ll://检查更新
                checkUpdate();
                break;
            case R.id.message_ll:

                break;
            case R.id.check_password_ll:
                startActivity(new Intent(context, NotifyPasswordActivity.class));
                break;
            case R.id.check_out_user_ll:
                UserUtil.clear();
                KongApp.finishAllActivity();
                startActivity(new Intent(context, LoginActivity.class));
                break;
        }
    }

    private void checkUpdate() {
        PgyUpdateManager.register(getActivity(),
                new UpdateManagerListener() {

                    @Override
                    public void onUpdateAvailable(final String result) {

                        // 将新版本信息封装到AppBean中
                        final AppBean appBean = getAppBeanFromString(result);
                        new AlertDialog.Builder(getActivity())
                                .setTitle("新版本")
                                .setMessage(appBean.getReleaseNote())
                                .setNegativeButton(
                                        "更新",
                                        new DialogInterface.OnClickListener() {

                                            @Override
                                            public void onClick(
                                                    DialogInterface dialog,
                                                    int which) {
                                                startDownloadTask(
                                                        getActivity(),
                                                        appBean.getDownloadURL());
                                            }
                                        }).show();
                    }

                    @Override
                    public void onNoUpdateAvailable() {
                        new AlertDialog.Builder(getActivity()).setTitle("检查更新").setMessage("您使用的已是最新版本!").setNegativeButton("确定", null).show();
                    }
                });
    }
}
